export class RequestInfo {
    path: string;
    method = 'GET';
    params?: any; // 发送查询请求需要的参数，如果结合listPage组件的输入属性selector，那么只需要传入其他未在selector中出现的参数
    option?: any;
    paramsGen?: () => any; // 生成params的方法，非必须
}

export class Level {
    title: string;
    placeholder: string;
    disabled = true;
    hidden: boolean;
    isFirstLevel: boolean;
    list = new Array<any>();
    selectedValue = null;
    selectedIdx = 0;
    loading?: boolean;   // 获取列表请求中
    isLeaf?: boolean;
    operating?: boolean; // CURD请求中
    child?: Level;
    reqInfo?: RequestInfo;
    reqProp?: string;
    labelFrom: string;
    valueFrom?: string;
    editing = false;
    http: any;

    [key: string]: any;

    handleSelect(selectedValue?: any, idx?: number) {
        this.selectedValue = selectedValue;
        this.selectedIdx = idx;
        if (!this.isLeaf) {
            if (selectedValue) {
                this.child.disabled = false;
                this.child.getList(selectedValue, idx);
            } else {
                this.child.disabled = true;
            }
            this.child.handleSelect(null);
        }
    }

    getList = (selectedValue?: any, idx?: number): void => {
        this.loading = true;
        this.list = null;
        this.http[(this.reqInfo.method || 'get').toLowerCase()](this.reqInfo.path, {
            ...(this.reqInfo.params),
            [this.reqProp]: selectedValue || ''
        }).then(rep => {
            this.loading = false;
            if (rep.status === 200) {
                this.encapsulateRep(rep);
            }
        });
    }

    encapsulateRep(rep) { // label value 格式封装
        let rst = [];
        if (Array.isArray(rep.result)) {
            rst = rep.result;
        } else {
            rst = rep.result.records;
        }
        this.list = rst.map(item => {
            return {
                label: item[this.labelFrom || 'label'],
                value: item[this.valueFrom || 'value'],
                ...item
            }
        });
    }

    constructor(options) {
        Object.assign(this, options);
    }
}

export class LevelControl {
    levels: Level[] = [];
    levelCasecsder = ({} as any);

    constructor(levels, http: any, options?: Level[]) {
        this.levels = levels;
        this.levelCasecsder = levels[0];
        let tmp = this.levelCasecsder;
        this.levels.map((item, index, list) => {
            // level 数组按序封装成嵌套结构
            item.http = http;
            if (index !== 0) {
                tmp.child = item;
                tmp = tmp.child;
                if (index === list.length - 1) {
                    tmp.isLeaf = true;
                }
            }
            if (index === 0) {
                item.isFirstLevel = true;
                item.disabled = false; // 默认第一个是可操作的
            } else if (index === list.length - 1) {
                item.isLeaf = true;
            }
        });
        if (Array.isArray(options)) {
            options.map((item, index) => {
                Object.assign(levels[index], item);
                if (levels[index].selectedValue) {
                    levels[index].handleSelect(levels[index].selectedValue);
                }
            });
        }
    }

    start() {
        this.levels[0].getList();
    }
}


